package com.example.administrator.myapplication.contact;

import android.Manifest;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.example.administrator.myapplication.MobileBook;
import com.example.administrator.myapplication.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by Administrator on 2018/11/2.
 */

public class ContanctActivity4 extends Activity {

    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tv);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                threadInfo();
//                    queryContactPhoneNumber();
                    queryContacts();


            }
        });
        checkPermissions();
//        handlemobilebookSucess();


    }
    private void checkPermissions() {
        List<String> permissionList = new ArrayList<>();
        if (ContextCompat.checkSelfPermission(ContanctActivity4.this,
                Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            permissionList.add(Manifest.permission.READ_CONTACTS);
        }
        if (!permissionList.isEmpty()) {
            String[] permissions = permissionList.toArray(new String[permissionList.size()]);
            ActivityCompat.requestPermissions(ContanctActivity4.this, permissions, 1);
        } else {


        }

    }

    private JSONArray contactData2;
    private JSONObject jsonObject2;
    private void queryContactPhoneNumber() throws JSONException {
        contactData2 = new JSONArray();

        String[] cols = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER};
        Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                cols, null, null, null);
        for (int i = 0; i < cursor.getCount(); i++) {
            cursor.moveToPosition(i);
            jsonObject2 = new JSONObject();
            contactData2.put(jsonObject2);
            // 取得联系人名字
            int nameFieldColumnIndex = cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
            int numberFieldColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            String name = cursor.getString(nameFieldColumnIndex);
            String number = cursor.getString(numberFieldColumnIndex);

            jsonObject2.put("name", name);
            jsonObject2.put("phone", number);
            Log.d("=====nnnn",name + "====="+ number);

        }
        Log.d("====json",contactData2.toString());
    }


    private void queryContacts() {
        //投影、映射
         String[] projection = {
                //电话的ID
            ContactsContract.CommonDataKinds.Phone._ID, //"_id"//display_name 显示的名字
             ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,// "display_name"
            // raw_contact_id字段
            ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID,
            //"raw_contact_id"
             ContactsContract.CommonDataKinds.Phone.MIMETYPE,
            // "mimetype"
             ContactsContract.CommonDataKinds.Phone.NUMBER,// "data1"
             ContactsContract.CommonDataKinds.Phone.TYPE // "data2"
        };
        // 获取用来操作数据的类的对象，对联系人的基本操作都是使用这个对象
        ContentResolver resolver  = getContentResolver();
        // 查询contacts表的所有记录
        Cursor cursorUser = resolver.query(ContactsContract.Data.CONTENT_URI, projection,
                null, null, null);

        //循环获取查询结果
         List<Map<String, Object>> list = new ArrayList<>();
         while (cursorUser.moveToNext()) {
        // 按上面projection数组的声明顺序获取获取对应字段的值
         int _id = cursorUser.getInt(0);
         String display_name = cursorUser.getString(1);
         int raw_contact_id = cursorUser.getInt(2);
        // 使用map保存数据
             Map<String, Object> item = new HashMap<>();
         item.put("_id", _id);
         item.put("display_name", display_name);
         item.put("raw_contact_id", raw_contact_id);
         item.put("mimitype", cursorUser.getString(3));
         item.put("data1", cursorUser.getString(4));
         item.put("data2", cursorUser.getInt(5));
         list.add(item);

         }
        Log.d("=========llist",list.toString());
        // 关闭Cursor
        cursorUser.close();
    }

}
